﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WeiTap.Data.Attributes;

namespace WeiTap.Data.Core
{
    public class ColumnInfo
    {
        public string ColumnName { get; set; }
        public string InsertTemplate { get; set; }
        public string UpdateTemplate { get; set; }
        public static ColumnInfo FromProperty(PropertyInfo propertyInfo) {
            var expliciyColumns =
                propertyInfo.DeclaringType.GetCustomAttributes<ExplicitColumnsAttribute>(true).Count()>0;
            var colAttrs =
               propertyInfo.GetCustomAttributes<ColumnAttribute>();
            if (expliciyColumns)
            {
                if (colAttrs.Count() == 0)
                    return null;
            }
            else {
                if (propertyInfo.GetCustomAttributes<IgnoreAttribute>(true).Count() != 0)
                    return null;
            }
            var ci = new ColumnInfo();
            if (colAttrs.Count() > 0)
            {
                var colattr = colAttrs.First();
                ci.InsertTemplate = colattr.InsertTemplate;
                ci.UpdateTemplate = colattr.UpdateTemplate;
                ci.ColumnName = colattr.Name == null ? propertyInfo.Name : colattr.Name;

            }
            else {
                ci.ColumnName = propertyInfo.Name;
            }
            return ci;
        }
    }
}
